package com.wyp.didishige.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wyp.didishige.config.Constants;
import com.wyp.didishige.utils.IntentUtil;
import com.wyp.didishige.wiget.xlist.XListView;
import com.wyp.didishige.R;

import com.wyp.didishige.activity.DynamicDetailActivity;
import com.wyp.didishige.model.Request;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

public class MyRequestFragment extends FragmentBase implements XListView.IXListViewListener {

    private static final String ARG_SECTION_NUMBER = "section_number";


    private XListView list;
    List<Request> mList;

    /**
     * The Adapter which will be used to populate the ListView/GridView with
     * Views.
     */
    private MyAdapter mAdapter;

    public static MyRequestFragment newInstance(int sectionNumber) {
        MyRequestFragment fragment = new MyRequestFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);

        return fragment;
    }

    public MyRequestFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mList=new ArrayList<Request>();
        mAdapter = new MyAdapter();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item, container, false);

        list = (XListView) view.findViewById(R.id.list);
        list.setPullLoadEnable(true);
        list.setPullRefreshEnable(false);
        list.setXListViewListener(this);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle=new Bundle();
                bundle.putString(Constants.DYNAMIC_ID,mList.get(--position).getObjectId());
                IntentUtil.start_activity(getActivity(), DynamicDetailActivity.class, bundle);
            }
        });

        list.setAdapter(mAdapter);
        onLoadMore();
        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {
        loadData();
    }
    int page=0;
    private void loadData(){
        BmobQuery<Request> query=new BmobQuery<Request>();
        query.addWhereEqualTo("shiMei",userManager.getCurrentUserName());
        query.order("-createdAt");

        query.setSkip(page*20);
        query.setLimit(20);
        query.findObjects(getActivity(), new FindListener<Request>(){
            @Override
            public void onSuccess(List<Request> requestItems) {
                if (requestItems!=null&&requestItems.size()!=0){
                    mList.addAll(requestItems);
                    mAdapter.notifyDataSetChanged();
                    page++;
                }else{
                  list.setPullLoadEnable(false);
                }
            }

            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onFinish() {
                super.onFinish();
                list.stopLoadMore();
            }
        });
    }

    class ViewHolder{
        public TextView item_time;
        public TextView item_text;
    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position--);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView==null){
                holder=new ViewHolder();
                convertView=mInflater.inflate(R.layout.request_item,null);
                holder.item_text= (TextView) convertView.findViewById(R.id.item_text);
                holder.item_time= (TextView) convertView.findViewById(R.id.item_time);
                convertView.setTag(holder);
            }else{
                holder= (ViewHolder) convertView.getTag();
            }

            Request item=mList.get(position);
            holder.item_time.setText(item.getCreatedAt());
            holder.item_text.setText(item.getTitle());
            return convertView;
        }
    }

}
